package cn.earl.springboot;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;


@ServletComponentScan
@EnableJpaRepositories // 不添加也行
@SpringBootApplication // @ComponentScan+@EnableAutoConfiguration+@SpringBootConfiguration....
// @EnableScheduling//开启定时器
@EnableAutoConfiguration(exclude={MultipartAutoConfiguration.class})
@EnableTransactionManagement // 开启事务管理器
public class YouzhuApplication extends WebMvcConfigurerAdapter {

	/**
	 * 添加json数据转换器
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		// super.configureMessageConverters(converters);实现为空
		/**
		 * 定义一个消息转换器，给消息转换器设置各种属性，最后添加到List<HttpMessageConverter<?>>
		 * converters中，最后在需要修改的实体类属性上使用@JSONField
		 */
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		// 配置fastJsonHttpMessageConverter中的各种属性
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		List<MediaType> fastMediaTypes = new ArrayList<>();
		// 中文乱码
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		// 格式化返回的json数据
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
		fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);

		// 添加到转换器列表中
		converters.add(fastJsonHttpMessageConverter);
	}

	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		// Set the default character encoding to use for parsing requests
		// default is iso-8859-1
		resolver.setDefaultEncoding("utf-8");
		// 推迟文件的解析
		resolver.setResolveLazily(true);
		// 字节
		resolver.setMaxInMemorySize(40960);
		// 限制上传文件的最大大小为3M
		// 8bit=1byte 1024byte=1KB 1024KB=1MB
		resolver.setMaxUploadSize(1024 * 1024 * 3);
		return resolver;
	}

	/**
	 * 请求拦截器，校验用户的有效性
	 */
	// @Override
	// public void addInterceptors(InterceptorRegistry registry) {
	// super.addInterceptors(registry);
	// registry.addInterceptor(new
	// UserHandlerInterceptor()).addPathPatterns("/user/*").excludePathPatterns("/user/regist");
	// }

	public static void main(String[] args) {
		SpringApplication.run(YouzhuApplication.class, args);
	}
}
